import BtnUI from '../core/base/BtnUI';

import { Player } from "@minecraft/server";

import { receive_land } from '../utils/Land';
import { LandList } from '../core/data/land/LandList'

const land_list = new LandList();

/**
* @param { Player } player
*/
function showMainLandUI(player) {
    // const land_utils_list = get_plot_array(5);

    const form = new BtnUI(player);

    form.__Title = "领地";

    form.add_btn('领取领地', "", function () {
        receive_land(player, land_list)
    });

    form.add_btn('领地列表', "", function () {
        showLandListUI(player);
    })

    form.show();
}

/**
* @param { Player } player
*/
function showLandListUI(player) {
    let land_data_list = land_list.get_all_plot_data()

    const form = new BtnUI(player);

    form.__Title = "领地列表";

    if (land_data_list != "") {
        land_data_list = JSON.parse(land_data_list)
        for (let i = 0; i < land_data_list.length; i++) {
            const land = land_data_list[i]

            form.add_btn(`序列号: ${i}|主人: ${land.admin}`, "", function () {
                const centerX = land.area.min.x + 144 / 2;
                const centerZ = land.area.min.z + 144 / 2;
                player.teleport({ x: centerX, y: 101, z: centerZ });
            })
        }
    }

    form.add_btn("返回", "", function () {
        showMainLandUI(player);
    })

    form.show();
}

export {
    showMainLandUI,
    showLandListUI
}